package com.zs.games.resultTool;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author LiChao
 */
@Data
@ApiModel(description = "返回信息")
public class CimResponse<T> implements Serializable {
    private static final long serialVersionUID = 3552164426176399511L;


    @ApiModelProperty(value = "返回业务码")
    private long code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "承载数据【该数据可为空，如果为空，仅判断【业务码-code】的值即可】")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public CimResponse(){

    }

    public static <T> CimResponse<T> ok() {
        return restResult(null, ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage());
    }

    public static <T> CimResponse<T> okData(T data) {
        return restResult(data, ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage());
    }

    public static <T> CimResponse<T> message(String message) {
        CimResponse<T> response = new CimResponse<>();
        response.setMessage(message);
        return response;
    }
    public static <T> CimResponse<T> status(boolean flag, ResultCodeEnum codeEnum) {
        return flag ? success(codeEnum) : fail(codeEnum);
    }

    public CimResponse<T> data(T data) {
        return restResult(data, ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage());
    }

    public CimResponse<T> data(Long code, String message) {
        CimResponse<T> response = new CimResponse<>();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }


    public CimResponse<T> errorInfo(ResultCodeEnum codeEnum) {
        CimResponse<T> response = new CimResponse<>();
        response.setCode(codeEnum.getCode());
        response.setMessage(codeEnum.getMessage());
        return response;
    }

    public CimResponse<T> errorInfo(Long code, String message) {
        CimResponse<T> response = new CimResponse<>();
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

    public static <T> CimResponse<T> fail(ResultCodeEnum codeEnum) {
        CimResponse<T> response = new CimResponse<>();
        response.setCode(codeEnum.getCode());
        response.setMessage(codeEnum.getMessage());
        return response;
    }

    public static <T> CimResponse<T> success(ResultCodeEnum codeEnum) {
        CimResponse<T> response = new CimResponse<>();
        response.setCode(codeEnum.getCode());
        response.setMessage(codeEnum.getMessage());
        return response;
    }

    public static <T> CimResponse<T> failData(ResultCodeEnum codeEnum, T data) {
        CimResponse<T> response = CimResponse.fail(codeEnum);
        response.setData(data);
        return response;
    }

    public static <T> CimResponse<T> restResult(T data, Long code, String msg) {
        CimResponse<T> apiResult = new CimResponse<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMessage(msg);
        return apiResult;
    }
}
